package com.zsk.shop.controller.gop;

import com.zsk.shop.common.annotion.NoPermission;
import com.zsk.shop.common.base.BaseResponse;
import com.zsk.shop.common.base.model.token.TokenInfoModel;
import com.zsk.shop.common.utils.DateUtils;
import com.zsk.shop.common.utils.EntityConvertUtils;
import com.zsk.shop.common.utils.TokenUtil;
import com.zsk.shop.common.utils.ToolUtil;
import com.zsk.shop.common.utils.excel.ExcelUtil;
import com.zsk.shop.enums.EnumWithdrawStatus;
import com.zsk.shop.model.excel.WithdrawInfoExportModel;
import com.zsk.shop.model.params.GopWithdrawApplyListParams;
import com.zsk.shop.model.results.GopWithdrawApplyRecordResult;
import com.zsk.shop.service.WithdrawApplyService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

/**
 * @author Ys
 * @version 1.0
 * @date 2021/5/17 14:25
 */
@RestController
@Api(tags = "GOP提现记录")
@RequestMapping("/gop/withdrawApply")
public class GopWithdrawApplyController {

    @Resource
    private WithdrawApplyService withdrawApplyService;

    @ApiOperation("提现记录")
    @PostMapping(value = "/getWithdrawApplyList.do")
    public BaseResponse<List<GopWithdrawApplyRecordResult>> getWithdrawApplyList(@RequestBody GopWithdrawApplyListParams params) {
        return withdrawApplyService.getWithdrawApplyList(params);
    }

    @ApiOperation("提现审核")
    @GetMapping(value = "/updateStatus.do")
    @ApiImplicitParams({@ApiImplicitParam(name = "id", value = "列表主键Id"),
            @ApiImplicitParam(name = "status", value = "2同意 3已拒绝"),
            @ApiImplicitParam(name = "reviewReason", value = "备注")})
    public BaseResponse updateStatus(Long id, Integer status, String reviewReason) {
        return withdrawApplyService.updateStatus(id, status, reviewReason);
    }

    @NoPermission
    @ApiOperation("MCP提现列表-待审核")
    @GetMapping("/waitWithdrawExport.do")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userName", value = "用户名"),
            @ApiImplicitParam(name = "phone", value = "手机号"),
            @ApiImplicitParam(name = "createTime", value = "开始时间"),
            @ApiImplicitParam(name = "endTime", value = "结束时间")})
    public void waitWithdrawExport(HttpServletResponse response, String userName, String phone, String createTime, String endTime, String token) {
        TokenInfoModel tokenInfoModel = TokenUtil.verifyToken(token);
        if (ToolUtil.isEmpty(tokenInfoModel)) {
            return;
        }
        GopWithdrawApplyListParams params = GopWithdrawApplyListParams.builder()
                .userName(userName).phone(phone).createTime(createTime).endTime(endTime).status(EnumWithdrawStatus.ON.getCode().toString()).build();
        List<GopWithdrawApplyRecordResult> result = withdrawApplyService.getWithdrawApplyList(params).getData();
        List<WithdrawInfoExportModel> results = EntityConvertUtils.convertAListToBList(result, WithdrawInfoExportModel.class);
        String fileName = "提现数据(导出时间：" + DateUtils.format(new Date(), DateUtils.YYYYMMddHHmm) + ")";
        if (!ToolUtil.isAllEmpty(createTime, endTime)) {
            fileName = "提现数据(" + createTime.replaceAll(":", "") + "-" + createTime.replaceAll(":", "") + ")";
        }
        ExcelUtil.writeExcel(response, results, fileName, new WithdrawInfoExportModel());
    }
}